package org.handsomezhi.shortlink.project.toolkit;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;

@Component
@RequiredArgsConstructor
public class RedisUtil {


    private final StringRedisTemplate stringRedisTemplate;

    /**
     * 设置键在当天23:59:59过期
     * @param key 键名
     */
    public void setExpiryAtEndOfDay(String key) {

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 获取今天的结束时间 23:59:59
        LocalDateTime endOfDay = LocalDateTime.of(now.toLocalDate(), LocalTime.MAX);

        // 计算从现在到23:59:59的剩余秒数
        long secondsUntilEndOfDay = ChronoUnit.SECONDS.between(now, endOfDay);

        // 设置键的过期时间
        stringRedisTemplate.expire(key, secondsUntilEndOfDay, TimeUnit.SECONDS);
    }
}
